package org.example.demo;

import com.google.gson.Gson;
import dao.StudentDao;
import pojo.Student;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
    private String message;

    public void init() {
        message = "wuaoyu";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        Student student = new Student("2023003","李白","男","11110",21L);
        StudentDao studentDao = new StudentDao();
        int flag = studentDao.addStudents(student);
//        创造一个hashmap
         HashMap<String, Object> rs = new HashMap<>();
        if (flag>0){
            rs.put("code",200);
            rs.put("msg","添加成功");
            rs.put("data",1);
        }else {
            rs.put("code",500);
            rs.put("msg","添加失败");
        }
//        使用gson产生json数据
         Gson gson = new Gson();
         String json = gson.toJson(rs);
//         响应给用户
         PrintWriter out = resp.getWriter();
         out.println(rs);


    }


    public void destroy() {
    }
}